package com.atjinggu.jinggumail.product.service.impl;

import com.atjinggu.jinggumail.common.constant.ProductConstant;
import com.atjinggu.jinggumail.product.dao.AttrAttrgroupRelationDao;
import com.atjinggu.jinggumail.product.dao.AttrDao;
import com.atjinggu.jinggumail.product.entity.AttrAttrgroupRelationEntity;
import com.atjinggu.jinggumail.product.entity.AttrEntity;
import com.atjinggu.jinggumail.product.service.AttrAttrgroupRelationService;
import com.atjinggu.jinggumail.product.service.AttrService;
import com.atjinggu.jinggumail.product.vo.AttrAttrGroupRelationVo;
import com.atjinggu.jinggumail.product.vo.AttrGroupWithAttrsVo;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atjinggu.jinggumail.common.utils.PageUtils;
import com.atjinggu.jinggumail.common.utils.Query;

import com.atjinggu.jinggumail.product.dao.AttrGroupDao;
import com.atjinggu.jinggumail.product.entity.AttrGroupEntity;
import com.atjinggu.jinggumail.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Autowired
    AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Autowired
    AttrDao attrDao;

    @Autowired
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), new QueryWrapper<AttrGroupEntity>());

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Integer catelogId) {
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper();
        String key = (String) params.get("key");
        if (catelogId == 0) {
            if (StringUtils.isNotBlank(key)) {
                wrapper.eq("attr_group_id", key).or().like("attr_group_name", key);
            }
        } else {
            wrapper.eq("catelog_id", catelogId);
            if (StringUtils.isNotBlank(key)) {
                wrapper.and((obj) -> {
                    obj.eq("attr_group_id", key).or().like("attr_group_name", key);
                });
            } else {

            }
        }
        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
        return new PageUtils(page);
    }

    @Override
    public PageUtils listAttrRelation(Map<String, Object> params, Integer attrGroupId) {
        //查询关联关系表
        IPage<AttrEntity> page = new Page();
        List<AttrAttrgroupRelationEntity> entities = attrAttrgroupRelationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrGroupId));
        if (CollectionUtils.isNotEmpty(entities)) {
            List<Long> attrIds = entities.stream().map(entity -> entity.getAttrId()).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(attrIds)) {
                page = attrDao.selectPage(new Query<AttrEntity>().getPage(params), new QueryWrapper<AttrEntity>().in("attr_id", attrIds));

            }
        }
        //查询属性信息
        return new PageUtils(page);
    }

    @Override
    public void attrRelationDelete(List<AttrAttrGroupRelationVo> relations) {
        if (CollectionUtils.isNotEmpty(relations)) {
            attrAttrgroupRelationDao.deleteBatchRelation(relations);
        }
    }

    @Override
    public PageUtils listNoAttrRelation(Map<String, Object> params, Integer attrGroupId) {
        //获取当前分组的分类Id
        AttrGroupEntity attrGroupEntity = this.baseMapper.selectById(attrGroupId);
        Long categoryId = attrGroupEntity.getCatelogId();
        //设置查询条件当前分类下的，基本属性
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<AttrEntity>().
                eq("catelog_id",categoryId).
                eq("attr_type", ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode());
        //获取当前分类下，所有已经分组的属性
        //1.获取当前分类下的，所有分组
        List<AttrGroupEntity> attrGroupEntities = this.baseMapper.
                selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", categoryId));
        if (CollectionUtils.isNotEmpty(attrGroupEntities)) {
            List<Long> attrGroupIds = attrGroupEntities.stream().map(entity -> entity.getAttrGroupId()).collect(Collectors.toList());
            //2.查询这些分组关联的所有属性
            List<AttrAttrgroupRelationEntity> relationEntities = attrAttrgroupRelationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().in("attr_group_id", attrGroupIds));
            if(CollectionUtils.isNotEmpty(relationEntities)) {
                List<Long> collect = relationEntities.stream().map(entity -> entity.getAttrId()).collect(Collectors.toList());
                if(CollectionUtils.isNotEmpty(collect)){
                    wrapper.notIn("attr_id",collect);
                }
            }
        }
        String key = (String) params.get("key");
        if (StringUtils.isNotBlank(key)) {
            wrapper.and(w -> {
                w.eq("attr_id", key).or().like("attr_name", key);
            });
        }
        IPage<AttrEntity> attrs = attrDao.selectPage(new Query<AttrEntity>().getPage(params), wrapper);
        return new PageUtils(attrs);
    }

    @Override
    public void saveAttrRelation(List<AttrAttrGroupRelationVo> relations) {
        List<AttrAttrgroupRelationEntity> collect = relations.stream().map(relation -> {
            AttrAttrgroupRelationEntity entity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(relation, entity);
            return entity;
        }).collect(Collectors.toList());
        attrAttrgroupRelationService.saveBatch(collect);
    }

    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        List<AttrGroupEntity> attrGroups = this.baseMapper.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        //2、查询所有属性
        List<AttrGroupWithAttrsVo> collect = attrGroups.stream().map(group -> {
            AttrGroupWithAttrsVo attrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(group,attrsVo);
            List<AttrEntity> attrs = attrService.getRelationAttr(attrsVo.getAttrGroupId());
            attrsVo.setAttrs(attrs);
            return attrsVo;
        }).collect(Collectors.toList());

        return collect;
    }

}